﻿namespace NET8.Domain.ViewModels;

/// <summary>
/// 微信相关
/// </summary>
public class WeChatViews
{
    ///
    public class AccessTokenModel
    {
        ///
        public string access_token { get; set; }

        ///
        public int expires_in { get; set; }

        ///
        public string refresh_token { get; set; }

        ///
        public string openid { get; set; }

        ///
        public string scope { get; set; }

        ///
        public string unionid { get; set; }

        ///
        public int errcode { get; set; }

        ///
        public string errmsg { get; set; }
    }

    ///
    public class SessionModel
    {
        ///
        public string session_key { get; set; }

        ///
        public string unionid { get; set; }

        ///
        public string openid { get; set; }

        ///
        public int errcode { get; set; }

        ///
        public string errmsg { get; set; }
    }

    ///
    public class WeChatPhone
    {
        /// <summary>
        /// 手机号
        /// </summary>
        public string phoneNumber { get; set; }

        /// <summary>
        /// 无区号手机号
        /// </summary>
        public string purePhoneNumber { get; set; }

        /// <summary>
        /// 区号
        /// </summary>
        public string countryCode { get; set; }
    }

    ///
    public class WeChatUser
    {
        ///
        public string unionId { get; set; }

        ///
        public string openId { get; set; }

        ///
        public string nickName { get; set; }

        ///
        public int gender { get; set; }

        ///
        public string city { get; set; }

        ///
        public string country { get; set; }

        ///
        public string province { get; set; }

        ///
        public string avatarUrl { get; set; }
    }

    ///
    public class PhoneView
    {
        ///
        public string phoneNumber { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string purePhoneNumber { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string countryCode { get; set; }
    }

    ///
    public class WxPayData
    {
        //采用排序的Dictionary的好处是方便对数据包进行签名，不用再签名之前再做一次排序
        private SortedDictionary<string, object> m_values = new();

        /**
        * 设置某个字段的值
        * @param key 字段名
         * @param value 字段值
        */

        public void SetValue(string key, object value)
        {
            m_values[key] = value;
        }

        /**
        * 根据字段名获取某个字段的值
        * @param key 字段名
         * @return key对应的字段值
        */

        public object GetValue(string key)
        {
            m_values.TryGetValue(key, out object o);
            return o;
        }

        /**
         * 判断某个字段是否已设置
         * @param key 字段名
         * @return 若字段key已被设置，则返回true，否则返回false
         */

        public bool IsSet(string key)
        {
            m_values.TryGetValue(key, out object o);
            if (null != o)
                return true;
            else
                return false;
        }

        /**
        * @将Dictionary转成xml
        * @return 经转换得到的xml串
        * @throws WxPayException
        **/

        public string ToXml()
        {
            //数据为空时不能转化为xml格式
            if (0 == m_values.Count)
            {
                //Log.Error(this.GetType().ToString(), "WxPayData数据为空!");
                //throw new WxPayException("WxPayData数据为空!");
            }

            var xml = "<xml>";
            foreach (KeyValuePair<string, object> pair in m_values)
            {
                //字段值不能为null，会影响后续流程
                if (pair.Value == null)
                {
                    //Log.Error(this.GetType().ToString(), "WxPayData内部含有值为null的字段!");
                    //throw new WxPayException("WxPayData内部含有值为null的字段!");
                }

                if (pair.Value.GetType() == typeof(int))
                {
                    xml += "<" + pair.Key + ">" + pair.Value + "</" + pair.Key + ">";
                }
                else if (pair.Value.GetType() == typeof(string))
                {
                    xml += "<" + pair.Key + ">" + "<![CDATA[" + pair.Value + "]]></" + pair.Key + ">";
                }
                else//除了string和int类型不能含有其他数据类型
                {
                    //Log.Error(this.GetType().ToString(), "WxPayData字段数据类型错误!");
                    //throw new WxPayException("WxPayData字段数据类型错误!");
                }
            }
            xml += "</xml>";
            return xml;
        }

        /**
        * @将xml转为WxPayData对象并返回对象内部的数据
        * @param string 待转换的xml串
        * @return 经转换得到的Dictionary
        * @throws WxPayException
        */

        public SortedDictionary<string, object> FromXml(string xml, string ApiKey)
        {
            if (string.IsNullOrEmpty(xml))
            {
                //Log.Error(this.GetType().ToString(), "将空的xml串转换为WxPayData不合法!");
                //throw new WxPayException("将空的xml串转换为WxPayData不合法!");
            }

            var xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            var xmlNode = xmlDoc.FirstChild;//获取到根节点<xml>
            var nodes = xmlNode.ChildNodes;
            foreach (var xn in nodes)
            {
                var xe = (XmlElement)xn;
                m_values[xe.Name] = xe.InnerText;//获取xml的键值对到WxPayData内部的数据中
            }

            try
            {
                CheckSign(ApiKey);//验证签名,不通过会抛异常
            }
            catch (Exception)
            {
                //throw new WxPayException(ex.Message);
            }

            return m_values;
        }

        /**
        * @Dictionary格式转化成url参数格式
        * @ return url格式串, 该串不包含sign字段值
        */

        public string ToUrl()
        {
            var buff = "";
            foreach (KeyValuePair<string, object> pair in m_values)
            {
                if (pair.Value == null)
                {
                    //Log.Error(this.GetType().ToString(), "WxPayData内部含有值为null的字段!");
                    //throw new WxPayException("WxPayData内部含有值为null的字段!");
                }

                if (pair.Key != "sign" && pair.Value.ToString() != "")
                {
                    buff += pair.Key + "=" + pair.Value + "&";
                }
            }
            buff = buff.Trim('&');
            return buff;
        }

        /**
        * @生成签名，详见签名生成算法
        * @return 签名, sign字段不参加签名
        */

        public string MakeSign(string ApiKey)
        {
            //转url格式
            var str = ToUrl();
            //在string后加入API KEY
            str += "&key=" + ApiKey;
            //MD5加密
            var md5 = MD5.Create();
            var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            var sb = new StringBuilder();
            foreach (var b in bs)
            {
                sb.Append(b.ToString("x2"));
            }
            //所有字符转为大写
            return sb.ToString().ToUpper();
        }

        /**
        *
        * 检测签名是否正确
        * 正确返回true，错误抛异常
        */

        public bool CheckSign(string ApiKey)
        {
            //如果没有设置签名，则跳过检测
            if (!IsSet("sign"))
            {
                return true;
            }
            //如果设置了签名但是签名为空，则抛异常
            else if (GetValue("sign") == null || GetValue("sign").ToString() == "")
            {
                //Log.Error(this.GetType().ToString(), "WxPayData签名存在但不合法!");
                //throw new WxPayException("WxPayData签名存在但不合法!");
            }

            //获取接收到的签名
            var return_sign = GetValue("sign").ToString();

            //在本地计算新的签名
            var cal_sign = MakeSign(ApiKey);

            if (cal_sign == return_sign)
            {
                return true;
            }

            //Log.Error(this.GetType().ToString(), "WxPayData签名验证错误!");
            //throw new WxPayException("WxPayData签名验证错误!");
            return false;
        }

        /**
        * @获取Dictionary
        */

        public SortedDictionary<string, object> GetValues()
        {
            return m_values;
        }
    }
}